# GScore 仓库镜像同步还原工具

# 默认插件目录
$PluginsDir = "gsuid_core\plugins"

Write-Host "开始还原 git remote url 到原始地址..." -ForegroundColor Green
Write-Host "插件目录: $PluginsDir"
Write-Host ""

# URL 映射
$UrlMap = @{
    "gsuid_core" = "https://github.com/Genshin-bots/gsuid_core"
    "XutheringWavesUID" = "https://github.com/Loping151/XutheringWavesUID"
    "RoverSign" = "https://github.com/tyql688/RoverSign"
    "DNAUID" = "https://github.com/tyql688/DNAUID"
    "GenshinUID" = "https://github.com/KimigaiiWuyi/GenshinUID"
    "StarRailUID" = "https://github.com/baiqwerdvd/StarRailUID"
    "ZZZeroUID" = "https://github.com/ZZZure/ZZZeroUID"
    "DeltaUID" = "https://github.com/Agnes4m/DeltaUID"
    "SayuStock" = "https://github.com/KimigaiiWuyi/SayuStock"
    "BlueArchiveUID" = "https://github.com/KimigaiiWuyi/BlueArchiveUID"
    "VAUID" = "https://github.com/Agnes4m/VAUID"
    "MajsoulUID" = "https://github.com/KimigaiiWuyi/MajsoulUID"
    "WzryUID" = "https://github.com/KimigaiiWuyi/WzryUID"
    "LOLegendsUID" = "https://github.com/KimigaiiWuyi/LOLegendsUID"
    "CS2UID" = "https://github.com/Agnes4m/CS2UID"
    "ScoreEcho" = "https://github.com/Loping151/ScoreEcho"
    "TodayEcho" = "https://github.com/Loping151/TodayEcho"
}

$Total = 0
$Success = 0
$Skipped = 0

function Write-ColorText {
    param([string]$Text, [string]$Color = "White")
    switch ($Color) {
        "Red" { Write-Host $Text -ForegroundColor Red }
        "Green" { Write-Host $Text -ForegroundColor Green }
        "Yellow" { Write-Host $Text -ForegroundColor Yellow }
        default { Write-Host $Text }
    }
}

$Plugins = @(
    "XutheringWavesUID", "RoverSign", "DNAUID", "GenshinUID", "StarRailUID",
    "ZZZeroUID", "DeltaUID", "SayuStock", "BlueArchiveUID", "VAUID",
    "MajsoulUID", "WzryUID", "LOLegendsUID", "CS2UID", "ScoreEcho", "TodayEcho"
)

# 处理 gsuid_core 本体
if (Test-Path ".git") {
    $currentUrl = git remote get-url origin 2>$null
    $targetUrl = $UrlMap["gsuid_core"]

    Write-Host "  当前: $currentUrl"
    Write-Host "  目标: $targetUrl"

    if ($currentUrl -like "*github.com*" -and $currentUrl -notlike "*gitcode*") {
        Write-ColorText "  ✓ 已是原始地址" "Green"
        $Success++
    } elseif ($currentUrl -like "*gitcode.com*" -or $currentUrl -like "*cnb.cool*") {
        git remote set-url origin $targetUrl 2>$null
        Write-ColorText "  ✓ 成功" "Green"
        $Success++
    } else {
        Write-ColorText "  ⊘ 未知地址，跳过" "Yellow"
        $Skipped++
    }
} else {
    Write-ColorText "  ⊘ 非 git 仓库或目录不存在" "Yellow"
    $Skipped++
}
Write-Host ""

# 检查并收集已存在的插件
$ExistingPlugins = @()
foreach ($plugin in $Plugins) {
    $pluginPath = Join-Path $PluginsDir $plugin
    if (Test-Path $pluginPath) {
        $ExistingPlugins += $plugin
    }
}

if ($ExistingPlugins.Count -eq 0) {
    Write-ColorText "未找到任何插件目录，请检查路径: $PluginsDir" "Yellow"
    exit 0
}

Write-Host "发现 $($ExistingPlugins.Count) 个插件目录，开始处理...`n"

# 处理插件
foreach ($plugin in $ExistingPlugins) {
    $pluginPath = Join-Path $PluginsDir $plugin

    Write-Host "处理插件: $plugin"

    if (-not (Test-Path (Join-Path $pluginPath ".git"))) {
        Write-ColorText "  ⊘ 非 git 仓库" "Yellow"
        $Skipped++
        Write-Host ""
        continue
    }

    Push-Location $pluginPath -ErrorAction SilentlyContinue
    $currentUrl = git remote get-url origin 2>$null

    if ([string]::IsNullOrEmpty($currentUrl)) {
        Write-ColorText "  ⊘ 无 origin remote" "Yellow"
        $Skipped++
        Pop-Location
        Write-Host ""
        continue
    }

    $targetUrl = $UrlMap[$plugin]
    Write-Host "  当前: $currentUrl"
    Write-Host "  目标: $targetUrl"

    if ($currentUrl -like "*github.com*" -and $currentUrl -notlike "*gitcode*") {
        Write-ColorText "  ✓ 已是原始地址" "Green"
        $Success++
        Pop-Location
        Write-Host ""
        continue
    } elseif ($currentUrl -like "*gitcode.com*" -or $currentUrl -like "*cnb.cool*") {
        git remote set-url origin $targetUrl 2>$null
        Write-ColorText "  ✓ 成功" "Green"
        $Success++
    } else {
        Write-ColorText "  ⊘ 未知地址，跳过" "Yellow"
        $Skipped++
    }

    Pop-Location
    Write-Host ""
}

$Total = $ExistingPlugins.Count + 1
Write-Host "========================================"
Write-Host "完成！总计: $Total | 成功: $Success | 跳过: $Skipped"
Write-Host "========================================"
